package com.goldman.aplt.base.config;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.goldman.aplt.base.interceptor.BusiTrxnInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

/**
 * Web配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    public static final String[] BUSI_PATTERNS = new String[]
            {"/pf/**","/us/**","/ac/**","/ap/**","/pay/**","/ad/**","/mk/**","/cb/**","/io/**",};//TODO 需要有更准确的抓取业务交易路径的方法
    @Autowired
    private ObjectMapper objectMapper;

    @Bean
    public BusiTrxnInterceptor busiTrxnInterceptor() {
        return new BusiTrxnInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //业务交易拦截器
        registry.addInterceptor(busiTrxnInterceptor()).addPathPatterns(BUSI_PATTERNS).order(0);
    }

    @PostConstruct
    public void myObjectMapper() {
        // 解决enum不匹配问题 默认值为false
        objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
    }
}